package equipment_kind

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipment_kind"
	equipment_kindReq "github.com/flipped-aurora/gin-vue-admin/server/model/equipment_kind/request"
)

type EquipmentKindService struct {
}

// CreateEquipmentKind 创建equipmentKind表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) CreateEquipmentKind(equipmentKind *equipment_kind.EquipmentKind) (err error) {
	err = global.GVA_DB.Create(equipmentKind).Error
	return err
}

// DeleteEquipmentKind 删除equipmentKind表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) DeleteEquipmentKind(equipmentKind equipment_kind.EquipmentKind) (err error) {
	err = global.GVA_DB.Delete(&equipmentKind).Error
	return err
}

// DeleteEquipmentKindByIds 批量删除equipmentKind表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) DeleteEquipmentKindByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]equipment_kind.EquipmentKind{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateEquipmentKind 更新equipmentKind表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) UpdateEquipmentKind(equipmentKind equipment_kind.EquipmentKind) (err error) {
	err = global.GVA_DB.Save(&equipmentKind).Error
	return err
}

// GetEquipmentKind 根据id获取equipmentKind表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) GetEquipmentKind(id uint) (equipmentKind equipment_kind.EquipmentKind, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&equipmentKind).Error
	return
}

// GetEquipmentKindInfo 获取equipmentKind表所有记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) GetEquipmentKindInfo() (list []equipment_kind.EquipmentKind, err error) {
	db := global.GVA_DB.Model(&equipment_kind.EquipmentKind{})
	var equipmentKinds []equipment_kind.EquipmentKind

	err = db.Find(&equipmentKinds).Error

	fmt.Println(equipmentKinds)

	return equipmentKinds, err
}

// GetEquipmentKindInfoList 分页获取equipmentKind表记录
// Author [piexlmax](https://github.com/piexlmax)
func (equipmentKindService *EquipmentKindService) GetEquipmentKindInfoList(info equipment_kindReq.EquipmentKindSearch) (list []equipment_kind.EquipmentKind, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&equipment_kind.EquipmentKind{})
	var equipmentKinds []equipment_kind.EquipmentKind
	// 如果有条件搜索 下方会自动创建搜索语句

	where := db.Where("1=1")

	if info.KindNum != 0 {
		where = where.Where("kind_num= ?", info.KindNum)
	}
	if info.KindName != "" {
		where = where.Where("kind_name=?", info.KindName)
	}

	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&equipmentKinds).Error
	return equipmentKinds, total, err
}
